﻿/*
Project:       
File:          Shop.js
Description:   
*               
*               
*               
Author:        Lanny
Date:          July,2014
version:       2.0
Modify:
 
                 
Comments:
* 1. 2014.07.17 重写了 ShopManager.htm
2. 把先前的ShopManager.aspx 换成了htm 页面
3. 去掉了 asp.net 的 color 第三方插件。现在使用的是JQuery 插件。
4. 2014.9.30 更改了路径

 
*/
//var url = '../../BusinessTier/Handlers/ShopManagerHandler.ashx?';
var url = '../../BusinessTier/Handlers/ShopHandler.ashx?';
var urlParams;

var success = "Success";
var error = "Error";
var repeat = "Repeat";
var noExist = "noExist";

function LoadCombobox(id, url) {
    $('#' + id).combobox({
        url: url,
        valueField: 'ShopID',
        textField: 'ShopName',
        editable: true,
        onLoadSuccess: function (data) { //加载完成后,设置选中第一项
            $(this).combobox("select", "100000");
            var allData = eval(data);
            $("#listAllShops").empty();
            for (var i = 0; i < allData.length; i++) {
                if (allData[i].ShopID != "100000") {
                    $("#listAllShops").append("<option value='" + allData[i].ShopID + "' style='color:" + allData[i].ShopColor + "'>" + allData[i].ShopName + "</option>");
                }
            }
        }
    });
}

$(function () {
    //绑定allShops所有店铺~
    //Start

    LoadCombobox("allShops", url + 'action=get');
    //End.

    //Add Shop into DB--Begin.
    $("#btnAddShopColor").click(function () {
        var shop = $("#allShops").combobox("getText");
        var color = $("#mycolor").val();
        if (shop == "选择店铺") {
            $.messager.alert("店铺异常", "亲，请选择一个店铺进行操作！");
        }
        else {
            urlParams = 'action=add&shopname=' + shop + '&color=' + color;
            $.post(url + urlParams, function (data) {
                if (data == success) {
                    $("#allShops").combobox("setText", shop);
                    $.messager.alert("店铺操作", "添加店铺 " + shop + "  成功！");

                    //Start
                    LoadCombobox("allShops", url + 'action=get');
                    //End.
                }
                else if (data == repeat) {
                    $.messager.alert("店铺异常", "亲，此店铺已经添加啦！请你重新添加，或者你可以点击右边的按钮，对这个店铺进行修改。谢谢！！");
                }
                else {
                    $.messager.alert("店铺异常", "操作店铺发生异常，请联系管理人员查看log。");
                }
            });
        }

    });

    //Add Shop into DB--End.

    //delete shop --begin.
    $("#btnDelShop").click(function () {
        var shopid = $("#listAllShops option:selected").val();
        var shopname = $('#listAllShops option:selected').text();
        urlParams = "action=delete&shopid=" + shopid;
        $.messager.confirm('警告', '确认要删除店铺吗？ /r/n 如果你删除此店铺，那么在 登记 界面此店铺相关的数据都不会显示出来，敬请谨慎', function (r) {
            if (r) {
                $.post(url + urlParams, function (data) {
                    if (data == success) {
                        $.messager.alert("店铺操作", "删除店铺[ " + shopname + " ]成功");
                        LoadCombobox("allShops", url + 'action=get');
                    }
                    else if (data == error) {
                        $.messager.alert("店铺操作", "删除店铺[ " + shopname + " ]失败，请检查log。");
                    }
                });
            }
        });
    });
    //delete shop --end.

    //Update shop--begin.
    $("#btnUpdateShopColor").click(function () {
        var shopname = $("#allShops").combobox("getText");
        var color = $("#mycolor").val();

        if (shopname == "选择店铺") {
            $.messager.alert("操作异常", "请正确的选中一个店铺！");
        }
        else {
            urlParams = "action=update&shopname=" + shopname + "&color=" + color;
            $.post(url + urlParams, function (data) {
                if (data == success) {
                    $.messager.alert("店铺操作", "更新店铺[ " + shopname + " ]成功");
                    LoadCombobox("allShops", url + 'action=get');
                }
                else if (data == error) {
                    $.messager.alert("店铺操作", "更新店铺[ " + shopname + " ]失败，请检查log。");
                }
                else if (data == noExist) {
                    $.messager.alert("店铺操作", "此店铺[ " + shopname + " ]不存在，请先添加此店铺。");
                }
            });
        }
    });
    //Update shop--end.

})